package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.units.AliyunOSSOperator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Objects;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/admin/common")
public class UploadController {
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {
        log.info("文件上传开始:{}", file);
        String string = UUID.randomUUID().toString();
        String substring = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf("."));
        String filename = string + substring;
        String upload = aliyunOSSOperator.upload(file.getBytes(), file.getOriginalFilename());
        return Result.success(upload);
    }
}
